package com.shuziren.service.impl;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuziren.abnormal.CustomException;
import com.shuziren.entity.InterviewPosition;
import com.shuziren.entity.dto.SelectInterviewPositionDto;
import com.shuziren.entity.dto.InterviewPositionDto;
import com.shuziren.entity.dto.UpdateinterviewPositionDto;
import com.shuziren.mapper.InterviewPositionMapper;
import com.shuziren.service.InterviewPositionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 面试职位服务实现类，提供对面试职位信息的增删改查操作
 */
@Service
public class InterviewPositionServiceImpl extends ServiceImpl<InterviewPositionMapper, InterviewPosition> implements InterviewPositionService {

    @Autowired
    private InterviewPositionMapper interviewPositionMapper;

    /**
     * 添加面试职位信息
     * @param interviewPositionDto 面试职位信息
     * @return 是否添加成功
     */
    @Override
    public boolean addInterviewPosition(InterviewPositionDto interviewPositionDto) {
        if (interviewPositionDto == null){
            throw new CustomException("添加失败");
        }
        InterviewPosition interviewPosition = new InterviewPosition();
        if (interviewPositionDto.getStatus() == 0 || interviewPositionDto.getStatus() == null){
            interviewPositionDto.setStatus(2);
        }
        BeanUtils.copyProperties(interviewPositionDto, interviewPosition);
        JSONArray RequiredSkills = JSONUtil.parseArray(interviewPositionDto.getRequiredSkills());
        interviewPosition.setRequiredSkills(RequiredSkills);
        try {
            int insert = interviewPositionMapper.insert(interviewPosition);
            if (insert <= 0){
                throw new CustomException("添加失败");
            }
        } catch (Exception e) {
            throw new CustomException("添加失败");
        }

        return true;
    }

    /**
     * 删除面试职位信息
     * @param id 面试职位ID
     * @return 是否删除成功
     */
    @Override
    public boolean deleteInterviewPosition(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("ID cannot be null");
        }
        return interviewPositionMapper.deleteById(id) > 0;
    }

    /**
     * 更新面试职位信息
     * @param updateinterviewPositionDto 面试职位信息
     * @return 是否更新成功
     */
    @Override
    public boolean updateInterviewPosition(UpdateinterviewPositionDto updateinterviewPositionDto) {
        if (updateinterviewPositionDto == null) {
            throw new CustomException("更新失败:更新数据不内为空");
        }
        InterviewPosition interviewPosition = new InterviewPosition();
        BeanUtils.copyProperties(updateinterviewPositionDto,  interviewPosition);
        if (!(updateinterviewPositionDto.getRequiredSkills().isEmpty())){
            JSONArray RequiredSkills = JSONUtil.parseArray(updateinterviewPositionDto.getRequiredSkills());
            interviewPosition.setRequiredSkills(RequiredSkills);
        }
        try {
            int update = interviewPositionMapper.updateById(interviewPosition);
            if (update <= 0){
                throw new CustomException("更新失败");
            }
        } catch (Exception e) {
            throw new CustomException("更新失败");
        }
        return true;
    }

    /**
     * 根据ID获取面试职位信息
     * @param id 面试职位ID
     * @return 对应的面试职位信息
     */
    @Override
    public InterviewPosition getInterviewPosition(Integer id) {
        if (id == null || id <= 0) {
            throw new CustomException("没有此职位");
        }
        return interviewPositionMapper.selectById(id);
    }

    /**
     * 根据部门级别状态职位名称查询面试职位列表
     * @param selectInterviewPositionDto 查询条件
     * @param
     * @return 面试职位列表
     */
    @Override
    public Page<InterviewPosition> listInterviewPositions(SelectInterviewPositionDto selectInterviewPositionDto) {
        if (selectInterviewPositionDto == null) {
            throw new CustomException("查询失败:查询条件不能为空(分页数,每页条数)");
        }
        Page<InterviewPosition> page = new Page<>(selectInterviewPositionDto.getPageNum(), selectInterviewPositionDto.getPageSize());


        LambdaQueryWrapper<InterviewPosition> wrapper = new LambdaQueryWrapper<>();
        if (selectInterviewPositionDto.getPositionName() != null && selectInterviewPositionDto.getPositionName() != ""){
            wrapper.like(InterviewPosition::getPositionName, selectInterviewPositionDto.getPositionName());
        }
        if (selectInterviewPositionDto.getDepartment() != null && selectInterviewPositionDto.getDepartment() != ""){
            wrapper.like(InterviewPosition::getDepartment, selectInterviewPositionDto.getDepartment());
        }
        if (selectInterviewPositionDto.getPositionLevel() != null && selectInterviewPositionDto.getPositionLevel() != ""){
            wrapper.eq(InterviewPosition::getPositionLevel, selectInterviewPositionDto.getPositionLevel());
        }
        if (selectInterviewPositionDto.getStatus() != null && selectInterviewPositionDto.getStatus() != 0){
            wrapper.eq(InterviewPosition::getStatus, selectInterviewPositionDto.getStatus());
        }

        try {
            return interviewPositionMapper.selectPage(page, wrapper);
        }catch (Exception e){
            throw new CustomException("查询失败,请联系管理员:18875694942");
        }
    }
}